const app = getApp();

function initInput() {
  const that = this;

  that.setData({
    maxlength: 10,
    totalMoney:0.01,
    payDone:false,//可支付状态
  })
}

function inputMoney(event) {
  const that = this;
  let moneyValue = that.data.moneyValue;
  let totalMoney = that.data.totalMoney;
  let value = event.detail.value;
  let setdata = {};
  let temp = value.split('.');

  setdata.moneyValue = value;

  if (temp.length > 2) { //输入多个（.）是不允许的
    setdata.moneyValue = moneyValue;
  } else if (temp.length == 1 && temp[0].length > 7) { //纯整数
    setdata.moneyValue = moneyValue;
  } else if (temp.length > 1 && !temp[0]) { //第一个直接输入（.）
    setdata.moneyValue = '0.';
  } else if (temp[1] && temp[1].length > 2) { //小数大于2位
    setdata.moneyValue = moneyValue;
  } else { //带有小数
    if (temp[0].length > 7) {
      setdata.moneyValue = moneyValue;
    }
  }

  if (Number(setdata.moneyValue) >= totalMoney){
    setdata.payDone = true;
  }else{
    setdata.payDone = false;
  }
  
  // 不能直接转为number类型，否则不能设置小数
  that.setData(setdata)
}

export default {
  initInput,
  inputMoney,
};